<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
    <style type="text/css">
        .layui-table-cell{height:40px;line-height:40px;}
    </style>
</head>
<body>
<!--搜索框+添加物业公司-->
<!--<div class="layui-form">-->
<!--    <div class="layui-form-item" style="margin-bottom: 30px;text-align: center;">-->
<!--        <div class="layui-inline">-->
<!--            &lt;!&ndash;<label class="layui-form-label">公司名称</label>&ndash;&gt;-->
<!--            <div class="layui-input-inline">-->
<!--                <input type="text" name="stu_name" placeholder="请输入物业公司名称" autocomplete="off" class="layui-input" />-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="layui-inline">-->
<!--            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search-stu-submit">-->
<!--                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>-->
<!--            </button>-->
<!--        </div>-->
<!--        &lt;!&ndash;<a class="layui-btn" id="add" style="float:right">添加物业公司</a>&ndash;&gt;-->
<!--    </div>-->
<!--</div>-->

<blockquote class="layui-elem-quote quoteBox mod_default_box">
    <form class="layui-form">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="stu_name" placeholder="请输入物业公司名称" autocomplete="off" class="layui-input" />
            </div>
            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search-stu-submit">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
        </div>
    </form>
</blockquote>

<!--数据列表-->
<table class="layui-hide" id="test" lay-filter="stu-table"></table>

<script type="text/html" id="stu-toolbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
</script>
<script type="text/html" id="checkboxTpl">
    <input type="checkbox" name="status" value="{{d.id}}" title="启用" lay-filter="lockDemo" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script src="../../js/layui/layui.js"></script>
<script src="../../js/layui/layui.all.js"></script>
<script src="../../js/token.js"></script>
<script src="../../js/config.js"></script>

<script>
    layui.use('table', function(){
        var $ = layui.$;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        table.render({
            elem: '#test'
            ,id:'stu-table-id'//重载需要
            ,url:HOST+'wuye/selectWuye'
            ,title:'物业公司表'
            ,cols: [[
                {type:'numbers', title: '序号'}
                ,{field:'wyname',align : 'center',width : 125, title: '物业公司名称'}
                ,{field:'wycode',align : 'center',width : 125,title: '物业编号'}
                ,{field:'fzr',width : 110,align : 'center', title: '负责人'}
                ,{field:'phone',width : 125,align : 'center', title: '联系方式'}
                ,{field:'address',align : 'center', width : 125,title: '公司地址'}
                ,{field:'message',align : 'center',width : 125,title: '公司简介'}
                ,{field:'logo',align : 'center', title: '公司logo',width : 125,templet: function (d) {
                        if (d.logo==null || d.logo=="" || d.logo==undefined){

                        }else{
                            return "<div><img src=\""+HOST+"upload/"+d.logo+"\" onclick=\"previewImgs(this)\"></div>";
                        }
                    }}
                ,{field:'status',align : 'center', title:'是否启用',width:110, templet: '#checkboxTpl', sort: true}
                ,{toolbar:"#stu-toolbar",align : 'center',width:120,title:"操作"}
            ]]
            ,page: true
            ,page: {
                curr: layui.data("movieList_page").index
            },
            done: (res, curr, count) => {
                layui.data("movieList_page", {
                    key: 'index',
                    value: curr
                });
            }
        });

        //添加物业公司
        $('#add').click(function () {
            layer.open({
                type:2
                ,title:"添加物业公司"
                ,area: ['893px', '600px'],
                content:("addwuye.html")
            });
        });

        //搜索操作
        form.on('submit(search-stu-submit)',function (data) {
            table.reload('stu-table-id',{
                where:{"name":data.field.stu_name}
                ,page:{
                    curr:1
                }
            });
            return false;
        });

        //表操作  监听操作
        table.on('tool(stu-table)',function (obj) {
            var data=obj.data;
            var event=obj.event;
            //删除
            if(event ==='del'){
                layer.confirm('确认删除该物业公司吗？',function (index) {
                    $.ajax({
                        url:HOST+"wuye/delwuye?id="+data.id
                        ,type:"GET"
                        ,dataType:"json"
                        ,success:function (data) {
                            if(data.code==0){
                                layer.msg("删除成功");
                                table.reload("stu-table-id");
                            }else{
                                layer.msg("删除失败");
                            }
                        }
                        ,error:function () {
                            console.log("ajax error");
                        }
                    });
                    layer.close(index);
                });
            }else if(event==='edit'){
                $('#logo').attr('src', "/tmp/"+data.logo);
                form.val('update-stu-form', {
                    "id":data.id,
                    "wyname": data.wyname,
                    "fzr": data.fzr,
                    "phone": data.phone,
                    "address": data.address,
                    "status": data.status,
                    "message" : data.message,
                    "logo" : data.logo
                });
                layer.open({
                    type:2
                    ,title:"编辑物业公司"
                    ,skin:'layui-layer-molv'
                    ,area: ['95%', '95%'],
                    content:("updatewuye.html?id="+data.id+"&wyname="+data.wyname+"&fzr="+data.fzr+"&phone="+data.phone+"&address="+encodeURIComponent(data.address)+"&message="+data.message+"&logo="+data.logo+"&wycode="+data.wycode)
                    //content:$('#update-stu-layer')
                });
            }
        });

        //编辑操作物业公司
        form.on('submit(update-stu-form-submit)',function(data){
            data.field.logo=$("#picture").val();
            $.ajax({
                url:HOST+"wuye/updatewuye"
                ,type:"POST"
                ,contentType: 'application/json'
                ,data:JSON.stringify(data.field)
                ,success:function (data) {
                    if(data.code==2){
                        layer.msg('该物业公司已存在，请重新输入！');
                    }
                    if(data.code==0){
                        layer.close(layer.index);
                        layer.msg('编辑成功');
                        table.reload('stu-table-id');
                        location.reload();
                    }
                }
                ,error:function () {
                    console.log("ajax error");
                }
            });
            return false;
        });

        //监听启用操作
        form.on('checkbox(lockDemo)', function(obj){
            //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
            var id = this.value;
            var status = obj.elem.checked;
            //console.log("状态："+status);
            $.ajax({
                url:HOST+"wuye/upStatus?id="+id
                ,type:"POST"
                ,contentType: 'application/json'
                ,success:function (data) {
                    if(data.status==1){
                        layer.close(layer.index);
                        layer.msg('操作成功');
                        table.reload('stu-table-id');
                        location.reload(true);
                    }
                }
                ,error:function () {
                    console.log("ajax error");
                }
            });
        });
    });
</script>
<script>
    //原图预览
    function previewImgs(obj) {
        var img = new Image();
        img.src = obj.src;
        //var height = img.height + 50; // 原图片大小
        //var width = img.width; //原图片大小
        var imgHtml = "<img src='" + obj.src + "' width='800px' height='500px'/>";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            area: [800 + 'px', 550 + 'px'],// area: [width + 'px',height+'px'],  //原图显示
            shadeClose:true,
            scrollbar: false,
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }
</script>

</body>
</html>